package com.example.myapplicationndk.tab;

import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.example.myapplicationndk.R;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

/**
 * 渐变tab-viewpager
 */
public class MainATabActivity extends AppCompatActivity {

    private TabLayout tab;

    private ViewPager2 mViewPager;
    private List<Fragment> mFragments;
    private LinearLayout mIndicatorContainer;
    private List<ColorTrackTextView> mIndicators = new ArrayList<>();
    private TabLayoutMediator mediator;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tab);


        mIndicatorContainer = findViewById(R.id.ll_bottom);

        tab = findViewById(R.id.tab);
        mViewPager = findViewById(R.id.viewpager);

        // 加速数据处理 prefetch
        ((RecyclerView) mViewPager.getChildAt(0)).getLayoutManager().setItemPrefetchEnabled(true);
        ((RecyclerView) mViewPager.getChildAt(0)).setItemViewCacheSize(2);


        initIndicator();

        mFragments = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
//            if (i == 1) {
//                mFragments.add(new ViewPagerFragment());
//                continue;
//            }
            mFragments.add(new NormalFragment());
        }

        final String[] titles = {"界面1", "界面2", "界面3"};
        mViewPager.setAdapter(new FragmentStateAdapter(getSupportFragmentManager(), getLifecycle()) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                //FragmentStateAdapter内部自己会管理已实例化的fragment对象。
                // 所以不需要考虑复用的问题
                return mFragments.get(position);
            }

            @Override
            public int getItemCount() {
                return mFragments.size();
            }
        });

        mViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                if (positionOffset > 0) {
                    // 获取左边
                    ColorTrackTextView left = mIndicators.get(position);
                    // 设置朝向
                    left.setDirection(ColorTrackTextView.Direction.DIRECTION_RIGHT);
                    // 设置进度  positionOffset 是从 0 一直变化到 1 不信可以看打印
                    left.setCurrentProgress(1 - positionOffset);

                    // 获取右边
                    ColorTrackTextView right = mIndicators.get(position + 1);
                    right.setDirection(ColorTrackTextView.Direction.DIRECTION_LEFT);
                    right.setCurrentProgress(positionOffset);
                }
            }


        });


        mediator = new TabLayoutMediator(tab, mViewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                //这里可以自定义TabView

            }
        });
        //要执行这一句才是真正将两者绑定起来
        mediator.attach();

    }

    /**
     * 初始化可变色的指示器
     */
    private void initIndicator() {
        for (int i = 0; i < 3; i++) {
            // 动态添加颜色跟踪的TextView
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.weight = 1;
            ColorTrackTextView colorTrackTextView = new ColorTrackTextView(this);
            // 设置两种颜色
            colorTrackTextView.setOriginColor(Color.BLACK);
            colorTrackTextView.setChangeColor(Color.RED);
            colorTrackTextView.setText("123465789");
            colorTrackTextView.setLayoutParams(params);
            // 把新的加入LinearLayout容器
            mIndicatorContainer.addView(colorTrackTextView);
            // 加入集合
            mIndicators.add(colorTrackTextView);
            if (i == 0) {
                ColorTrackTextView left = mIndicators.get(0);
                // 设置朝向
                left.setDirection(ColorTrackTextView.Direction.DIRECTION_RIGHT);
                // 设置进度  positionOffset 是从 0 一直变化到 1 不信可以看打印
                left.setCurrentProgress(1 - 0);
            }
        }
    }
}